package org.qu.util.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 用户连接类
 * @author 韩进城
 *
 */
public class ConnectionData {
	/**
	 * 用户名
	 */
	public static final String USERNAME="root";
	/**
	 * 密码
	 */
	public static final String USERPWD="mldnadmin";
	/**
	 * 连接地址
	 */
	public static final String URL="jdbc:mysql://localhost:3306/qu";
	private Connection conn;
	//加载驱动
	static{
		try {
			Class.forName("com.mysql.jdbc.Driver");
			System.out.println("加载数据库驱动");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 取得连接
	 * @return
	 */
	public Connection getConnection(){
		if(conn==null){
			try {
				conn=DriverManager.getConnection(URL, USERNAME, USERPWD);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return conn;
	}
	/**
	 * 关闭连接
	 */
	public void closeConnection(){
		if(conn!=null){
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
}
